const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const productController = require('../controllers/productController');

// 创建商品
router.post('/', [
    body('name').notEmpty().withMessage('Name is required'),
    body('price').isFloat({ min: 0 }).withMessage('Price must be a positive number')
], productController.createProduct);

// 获取所有商品
router.get('/', productController.getAllProducts);

// 根据ID获取商品
router.get('/:id', productController.getProductById);

// 更新商品信息
router.put('/:id', [
    body('name').optional().notEmpty().withMessage('Name cannot be empty'),
    body('price').optional().isFloat({ min: 0 }).withMessage('Price must be a positive number')
], productController.updateProduct);

// 删除商品
router.delete('/:id', productController.deleteProduct);

module.exports = router;